package Json.search;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import utils.PrintUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Author: Jie
 * @Date: 2019/6/6 15:30
 * @Function :
 */
public class searchMain {
    private static final String TAG = "searchMain";
    private static List<SearchBean> searchBeanList;

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        int length = getNumLength(Integer.MAX_VALUE);


        System.out.println("length=="+length);
        String str = "[\n" +
                "  {\n" +
                "    \"name\": \"网络0\",\n" +
                "    \"menuType\": 1,\n" +
                "    \"intentPath\": \"NetSettingActivity\",\n" +
                "    \"mSonList\":null,\n" +
                "\"iconName\":\"net icon\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"name\": \"人员0\",\n" +
                "    \"menuType\": 1,\n" +
                "    \"intentPath\": \"NetSettingActivity\",\n" +
                "\"iconName\":\" icon 人员0\",\n" +
                "    \"mSonList\":[\n" +
                "      {\n" +
                "        \"name\": \"管理员1\",\n" +
                "        \"menuType\": 1,\n" +
                "        \"intentPath\": \"DeviceManagerActivity\",\n" +
                "        \"mSonList\":null\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"人员管理1\",\n" +
                "        \"menuType\": 1,\n" +
                "        \"intentPath\": \"\",\n" +
                "\"iconName\":\" icon 人员管理 1\",\n" +
                "        \"mSonList\":[\n" +
                "          {\n" +
                "            \"name\": \"管理员 Son2\",\n" +
                "            \"menuType\": 1,\n" +
                "            \"intentPath\": \"DeviceManagerActivity\",\n" +
                "            \"mSonList\":null\n" +
                "          },\n" +
                "          {\n" +
                "            \"name\": \"人员管理 Son2\",\n" +
                "            \"menuType\": 1,\n" +
                "            \"intentPath\": \"\",\n" +
                "            \"mSonList\":[\n" +
                "              {\n" +
                "                \"name\": \"网络3\",\n" +
                "                \"menuType\": 1,\n" +
                "                \"intentPath\": \"NetSettingActivity\",\n" +
                "                \"mSonList\":null\n" +
                "              },\n" +
                "              {\n" +
                "                \"name\": \"人员3\",\n" +
                "                \"menuType\": 1,\n" +
                "                \"intentPath\": \"NetSettingActivity\",\n" +
                "                \"mSonList\":[\n" +
                "                  {\n" +
                "                    \"name\": \"管理员4\",\n" +
                "                    \"menuType\": 1,\n" +
                "                    \"intentPath\": \"DeviceManagerActivity\",\n" +
                "                    \"mSonList\":null\n" +
                "                  },\n" +
                "                  {\n" +
                "                    \"name\": \"人员管理4\",\n" +
                "                    \"menuType\": 1,\n" +
                "                    \"intentPath\": \"\",\n" +
                "                    \"mSonList\":[\n" +
                "                      {\n" +
                "                        \"name\": \"管理员 Son 5\",\n" +
                "                        \"menuType\": 1,\n" +
                "                        \"intentPath\": \"DeviceManagerActivity\",\n" +
                "                        \"mSonList\":null\n" +
                "                      },\n" +
                "                      {\n" +
                "                        \"name\": \"人员管理 Son5\",\n" +
                "                        \"menuType\": 1,\n" +
                "                        \"intentPath\": \"\",\n" +
                "                        \"mSonList\":null\n" +
                "                      }\n" +
                "                    ]\n" +
                "                  }\n" +
                "                ]\n" +
                "              }\n" +
                "            ]\n" +
                "          }\n" +
                "        ]\n" +
                "      }\n" +
                "    ]\n" +
                "  },\n" +
                "  {\n" +
                "    \"name\": \"人员22 0\",\n" +
                "    \"menuType\": 1,\n" +
                "    \"intentPath\": \"NetSettingActivity\",\n" +
                "\"iconName\":\" icon 人员22 0\",\n" +
                "    \"mSonList\":[\n" +
                "      {\n" +
                "        \"name\": \"管理员22 1\",\n" +
                "        \"menuType\": 1,\n" +
                "        \"intentPath\": \"DeviceManagerActivity\",\n" +
                "\"iconName\":\" icon 管理员22 1\",\n" +
                "        \"mSonList\":null\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"人员管理22 1\",\n" +
                "        \"menuType\": 1,\n" +
                "        \"intentPath\": \"\",\n" +
                "        \"mSonList\":[\n" +
                "          {\n" +
                "            \"name\": \"管理员22  Son2\",\n" +
                "            \"menuType\": 1,\n" +
                "            \"intentPath\": \"DeviceManagerActivity\",\n" +
                "            \"mSonList\":null\n" +
                "          },\n" +
                "          {\n" +
                "            \"name\": \"人员管理22  Son2\",\n" +
                "            \"menuType\": 1,\n" +
                "\"iconName\":\" icon 人员管理22  Son2\",\n" +
                "            \"intentPath\": \"\",\n" +
                "            \"mSonList\":[\n" +
                "              {\n" +
                "                \"name\": \"网络22 3\",\n" +
                "                \"menuType\": 1,\n" +
                "                \"intentPath\": \"NetSettingActivity\",\n" +
                "                \"mSonList\":null\n" +
                "              },\n" +
                "              {\n" +
                "                \"name\": \"人员22  3\",\n" +
                "                \"menuType\": 1,\n" +
                "                \"intentPath\": \"NetSettingActivity\",\n" +
                "                \"mSonList\":[\n" +
                "                  {\n" +
                "                    \"name\": \"管理员22 4\",\n" +
                "                    \"menuType\": 1,\n" +
                "                    \"intentPath\": \"DeviceManagerActivity\",\n" +
                "                    \"mSonList\":null\n" +
                "                  },\n" +
                "                  {\n" +
                "                    \"name\": \"人员管理22 4\",\n" +
                "                    \"menuType\": 1,\n" +
                "                    \"intentPath\": \"\",\n" +
                "                    \"mSonList\":[\n" +
                "                      {\n" +
                "                        \"name\": \"管理员22  Son 5\",\n" +
                "                        \"menuType\": 1,\n" +
                "                        \"intentPath\": \"DeviceManagerActivity\",\n" +
                "                        \"mSonList\":null\n" +
                "                      },\n" +
                "                      {\n" +
                "                        \"name\": \"人员管理22  Son5\",\n" +
                "                        \"menuType\": 1,\n" +
                "                        \"intentPath\": \"\",\n" +
                "                        \"mSonList\":null\n" +
                "                      }\n" +
                "                    ]\n" +
                "                  }\n" +
                "                ]\n" +
                "              }\n" +
                "            ]\n" +
                "          }\n" +
                "        ]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "]";

        parseData(str);
        Scanner scanner = new Scanner(System.in);
        System.out.println(searchBeanList.size());
        while (true) {
            // 第n个斐波那契数,从0开始
            String input = scanner.next();
            mcount = 0;
            search(input);
            mPrint(resList);
        }

    }

    private static void mPrint(List<SearchBean> resList) {
        for (SearchBean bean : resList) {
            String icon = findIcon(bean);
            bean.setIconName(icon);
            System.out.println("name "+bean.getName()+"  icon=="+ icon);
        }
    }

    private static String findIcon(SearchBean bean) {
        if (bean == null) {
            return null;
        }
        if (bean.getIconName() != null && bean.getIconName().length()> 0){
            return bean.getIconName();
        }
        if (bean.getParentNode() == null){
            return null;
        }
        if (bean.getParentNode()!=null){
            return findIcon(bean.getParentNode());
        }
        return null;
    }

    private static List<SearchBean> resList = new ArrayList<>();
    private static void search(String input) {
        resList.clear();
        if (input ==null || input.length() == 0  || input.trim().length() == 0){
            System.err.println("input == null");
            return;
        }
        for (SearchBean bean : searchBeanList) {
            search(bean,input);
        }

    }

    static int mcount =0;
    private static void search(SearchBean bean, String input) {
        System.out.println("search "+(mcount++));
        if (bean == null) {
            return;
        }
        if (bean.getName()!=null &&bean.getName().contains(input)){
            resList.add(bean);
        }
//        if (bean.getmSonList() != null && bean.getmSonList().size()>0) {
//            for (SearchBean sonBean : bean.getmSonList()) {
//                search(sonBean,input);
//            }
//        }
    }

    private static int getNumLength(int maxValue) {
        if (maxValue==0){
            return 0;
        }
        return 1+ getNumLength(maxValue/10);
    }

    private static void parseData(String searchData) {
        searchBeanList = new ArrayList<>();
        Gson gson = new Gson();
            if (searchData == null || searchData.length()==0|| searchData.trim().length()==0){
                return;
            }
            try {
                List<SearchBean> searchBeanList =  gson.fromJson(searchData,new TypeToken<List<SearchBean>>(){}.getType());
                for (int i = 0; i < searchBeanList.size(); i++) {
                    SearchBean searchBean = searchBeanList.get(i);
                    searchBeanNode(null,searchBean);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    private static void searchBeanNode(SearchBean pro, SearchBean searchBean) {
        if (searchBean == null) {
            return;
        }
        searchBeanList.add(searchBean);
        searchBean.setParentNode(pro);
        if (searchBean.getmSonList() != null && searchBean.getmSonList().size()>0) {
            for (SearchBean son :searchBean.getmSonList() ) {
                searchBeanNode(searchBean,son);
            }
        }
    }



    private static void printSearchBean(String i, SearchBean searchBean) {
        if (searchBean == null){
            return;
        }
        System.out.println(i +"   "+ searchBean.toString());
        if (searchBean.getmSonList() != null) {
            for (SearchBean bean : searchBean.getmSonList()) {
                printSearchBean(i+1,bean);
            }
        }
    }

    /**
     * 补全int 为2位数
     * @param num
     * @return
     */
    private static String appendIntToSecond(int num){
     if (num==0){
         return "00";
     }
     if (num<10){
         return "0"+String.valueOf(num);
     }
     if (num>99){
         throw new RuntimeException("num > 99 is illegal");
     }
     return String.valueOf(num);
    }

}
